package com.dao.impl;

import com.entity.MongoDBDocument;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

//@Repository
public class MongodbDaoImpl {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 新增文档
     *
     * @param mongoDBDocument
     * @return
     */
    public void insert(MongoDBDocument mongoDBDocument) {
        //insert方法并不提供级联类的保存，所以级联类需要先自己先保存
        mongoTemplate.insert(mongoDBDocument);
    }

    public void save(MongoDBDocument mongoDBDocument) {
        Sort sort = new Sort(Sort.Direction.DESC, "name");
        mongoDBDocument = mongoTemplate.findOne(Query.query(Criteria.where("")).with(sort), MongoDBDocument.class);
        mongoTemplate.save(mongoDBDocument);
    }

    /**
     * 删除文档
     * NOTE:remove方法不支持级联删除所以要单独删除子数据
     * @param name
     */
    public void remove(String name) {
        //根据名字查询数据并删除
        MongoDBDocument mongoDBDocument = mongoTemplate.findOne(Query.query(Criteria.where("name").is(name)), MongoDBDocument.class);
        //remove方法不支持级联删除所以要单独删除子数据
        List<MongoDBDocument> metMongoDBDocuments = mongoDBDocument.getMongoDBDocuments();
        for (MongoDBDocument metMongoDBDocument : metMongoDBDocuments) {
            mongoTemplate.remove(metMongoDBDocument);
        }
        //删除主数据
        mongoTemplate.remove(mongoDBDocument);
    }

    /**
     * 更新文档
     * @param mongoDBDocument
     */
    public void update(MongoDBDocument mongoDBDocument) {
//        mongoTemplate.updateFirst(Query.query(Criteria.where("name").is(mongoDBDocument.getName())), Update.update("age", mongoDBDocument.getAge()), MongoDBDocument.class);
    }

    /**
     * 查询文档
     * @param name
     */
    public void find(String name) {
        Sort sort = new Sort(Sort.Direction.DESC, "name");
        List<MongoDBDocument> mongoDBDocumentS = mongoTemplate.find(Query.query(Criteria.where("name").is(name)), MongoDBDocument.class);
        //基于sort排序使用findOne查询最新一条记录
        MongoDBDocument mongoDBDocument = mongoTemplate.findOne(Query.query(Criteria.where("name").is(name)).with(sort), MongoDBDocument.class);
        //模糊查询
        List<MongoDBDocument> mongoDBDocumentList = mongoTemplate.find(Query.query(Criteria.where("name").is(name).regex(name)).with(sort), MongoDBDocument.class);
        //分页查询
        Pageable pageable = new PageRequest(3, 20, sort);//换spring 5要换成of方法
        List<MongoDBDocument> mongoDBDocumentPageableList = mongoTemplate.find(Query.query(Criteria.where("name").is(name)).with(pageable), MongoDBDocument.class);
        //总数
        long conut = mongoTemplate.count(Query.query(Criteria.where("name").is(name)), MongoDBDocument.class);
        Page<MongoDBDocument> pages = new PageImpl(mongoDBDocumentPageableList, pageable, conut);

    }
    //根据uid分页查询
    public List<MongoDBDocument> page(int page,String uid){
        Query query = new Query();
        query.addCriteria(Criteria.where("uid").is(uid));
        query.with(new Sort(new Sort.Order(Sort.Direction.DESC,"timestamp")));
        query.limit(20);
        query.skip((page-1)*20);
        return mongoTemplate.find(query,MongoDBDocument.class);

    }

}